import { createRouter, createWebHistory } from 'vue-router'
import home from '@/views/home.vue'
import register from '@/views/register.vue'
import login from '@/views/login.vue'
import mycontent from '@/views/mycontent.vue'
import account from '@/views/account.vue'
import password from '@/views/password.vue'
import privacy from '@/views/privacy.vue'
import deleteaccount from '@/views/deleteaccount.vue'
import tag_management from '@/views/tag_management.vue'
import type_management from '@/views/type_management.vue'
import article from '@/views/article.vue'
import blog from '@/views/blog.vue'
import video from '@/views/video.vue'
import industry from '@/views/industry.vue'
import blogArticle from '@/views/blogArticle.vue'
import upload from '@/views/upload.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/home',
      name: 'home',
      component: home,
      children:[
        {
          path:'blog',
          component:blog
        },
        {
          path:'video',
          component:video
        },
        {
          path:'industry',
          component:industry
        }
      ]
    },
    {
      path: '/register',
      name: 'register',
      component: register
    },
    {
      path: '/login',
      name: 'login',
      component: login
    },
    {
      path:'/mycontent',
      name:'mycontent',
      component:mycontent,
      children:[
        {
          path:'account',
          component:account
        },
        {
          path:'password',
          component:password
        },
        {
          path:'privacy',
          component:privacy
        },
        {
          path:'delete-account',
          component:deleteaccount
        },
        {
          path:'tag_management',
          component:tag_management
        },
        {
          path:'type_management',
          component:type_management
        },
        {
          path:'article',
          component:article
        }
      ]
    },
    {
      path:'/home/blog/:id',
      name:'blogArticle',
      component:blogArticle
    },
    {
      path:'/upload/',
      name:'upload',
      component:upload
    }
  ],
})
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.meta.requiresAuth && !token) {
    next('/login')
  } else {
    next()
  }
})
export default router
